#include "Parser.h"

using namespace Accesorios;

Parser::Parser() {
}

unsigned int Parser::cantidadElementos(string* linea, const char separador) {
	unsigned int numElements = 0;
	unsigned int i = 0;
	while (i < linea->size()) {
		if (linea->at(i) == separador)
			numElements++;
		i++;
	}
	return numElements;
}

void Parser::reemplazarCaracter(string* linea, const char caracterActual, const char caracterNuevo) {
	replace(linea->begin(), linea->end(), caracterActual, caracterNuevo);
}

vector<string>* Parser::separarCampos(string* linea, const char separador) {
	vector<string>* toReturn = new vector<string> ();
	unsigned int i = 0;
	unsigned int j = 0;
	string campo;

	unsigned int numElements = Parser::cantidadElementos(linea, separador);
	if(numElements != 0){
		while (i < numElements) {
			while (j < linea->size()) {
				if (linea->at(j) != separador) {
					campo.push_back(linea->at(j));
					j++;
				} else if (linea->at(j) == separador) {
					toReturn->push_back(campo);
					campo.clear();
					j++;
					i++;
				}
			}
			toReturn->push_back(campo);
			campo.clear();
			i++;
		}
	}else{
		toReturn->push_back(*linea);
	}
	return toReturn;
}

Parser::~Parser() {
}

